package com.kakaxi.mybilibili.module.home.category.view;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.pager.BaseFragmentPagerAdapter;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.base.MyApplication;
import com.kakaxi.mybilibili.base.NavigationActivity;
import com.kakaxi.mybilibili.bean.CategoryHomeItemBean;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.module.search.view.SearchActivity;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 分类分区活动类
 * Created by kakaxi on 16/11/28.
 */

public class CategoryAreaActivity extends NavigationActivity {

    @BindView(R.id.tablayout)
    TabLayout tabLayout;

    @BindView(R.id.viewpager)
    ViewPager viewPager;

    /** 子数组的共享参数名 **/
    public static final String EXTRA_SHARE_CHILD_LIST = "categoryHomeChildList";
    /** 区名传递参数名 **/
    public static final String EXTRA_TYPENAME = "typename";
    /** 区id传递参数名 **/
    public static final String EXTRA_TID = "tid";

    /** 区名 **/
    private String mTypename;
    /** 区id **/
    private int mTid;

    private List<CategoryHomeItemBean> mChildList;

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_common_tab_pager;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        showBackBtn();
    }

    @Override
    protected void initData() {
        mTypename = getIntent().getStringExtra(EXTRA_TYPENAME);
        mTid = getIntent().getIntExtra(EXTRA_TID, -1);

        if (mTypename != null) {
            setTitle(mTypename);
        }

        Object object = ((MyApplication) getApplication()).getShareData
                (EXTRA_SHARE_CHILD_LIST);
        initTabPager(object);
    }

    /**
     * 初始化标签滑动布局
     *
     * @param object 数据对象
     */
    private void initTabPager(Object object) {
        mChildList = CastUncheckedUtil.cast(object);

        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(CategoryAreaFirstFragment.newInstance(mTypename, mTid));
        for (CategoryHomeItemBean itemBean : mChildList) {
            fragmentList.add(CategoryAreaChildFragment.newInstance(itemBean.getTypename(),
                    itemBean.getTid()));
        }

        viewPager.setAdapter(new BaseFragmentPagerAdapter<>(getSupportFragmentManager(),
                fragmentList));
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 事件巴士控制滑动标签
     *
     * @param object 事件巴士对象
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onTurnPageEventBus(EventBusObject object) {
        if (object.getEvent() == EventBusObject.EVENT_CATEGORY_AREA_ACT_TURN_PAGE) {
            viewPager.setCurrentItem(1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_common, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                Bundle bundle = new Bundle();
                bundle.putInt(SearchActivity.EXTRA_TYPE, SearchActivity.TYPE_VIDEO);
                goActivity(SearchActivity.class, bundle);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
